using UnityEngine;
using UnityEngine.Networking;

[NetworkSettings(channel = 0, sendInterval = 1f)]
public class ScoreManager : NetworkBehaviour
{
	public bool Toggle;

	private GUIKillBadgeManager guiBadgeManager;

	private static int kCmdCmdScoreUpdate;

	private static int kRpcRpcScoreReceived;

	private void Start()
	{
		guiBadgeManager = (GUIKillBadgeManager)Object.FindObjectOfType(typeof(GUIKillBadgeManager));
	}

	private void Update()
	{
		if (UnityEngine.Input.GetKeyDown(KeyCode.Tab) && (bool)UnitZ.gameManager)
		{
			Toggle = !Toggle;
		}
	}

	private void UpdatePlayerScore(int id, int score, int dead)
	{
		if (base.isServer && !(UnitZ.NetworkGameplay == null) && !(UnitZ.NetworkGameplay.playersManager == null))
		{
			UnitZ.NetworkGameplay.playersManager.AddScore(id, score, dead);
		}
	}

	public void AddScore(int score, int id)
	{
		UpdatePlayerScore(id, score, 0);
	}

	public void AddDead(int dead, int id)
	{
		UpdatePlayerScore(id, 0, dead);
	}

	public void UpdateScore(int killer, int victim, int score)
	{
		if (base.isServer)
		{
			CallCmdScoreUpdate(killer, victim, score);
		}
	}

	[Command(channel = 0)]
	private void CmdScoreUpdate(int killer, int victim, int score)
	{
		if (victim != -1)
		{
			UpdatePlayerScore(victim, 0, 1);
		}
		if (killer != -1)
		{
			UpdatePlayerScore(killer, score, 0);
		}
		int score2 = 0;
		PlayersManager playersManager = UnitZ.NetworkGameplay.playersManager;
		if (playersManager != null)
		{
			PlayerData playerData = playersManager.GetPlayerData(killer);
			score2 = playerData.Score;
		}
		CallRpcScoreReceived(killer, victim, score2);
	}

	[ClientRpc(channel = 0)]
	private void RpcScoreReceived(int killer, int victim, int score)
	{
		AddKillText(killer, victim, "Kill", score);
	}

	public void AddKillText(int killer, int victim, string killtype, int killerscore)
	{
		if (guiBadgeManager == null)
		{
			guiBadgeManager = (GUIKillBadgeManager)Object.FindObjectOfType(typeof(GUIKillBadgeManager));
		}
		if (UnitZ.NetworkGameplay == null || UnitZ.NetworkGameplay.playersManager == null)
		{
			return;
		}
		PlayersManager playersManager = UnitZ.NetworkGameplay.playersManager;
		if (playersManager != null)
		{
			PlayerData playerData = playersManager.GetPlayerData(killer);
			PlayerData playerData2 = playersManager.GetPlayerData(victim);
			string text = "N/A";
			string victim2 = "N/A";
			if (killer == -1)
			{
				playerData.Name = "Player";
			}
			if (victim == -1)
			{
				playerData2.Name = "Player";
			}
			if (playerData.Name != string.Empty)
			{
				text = playerData.Name;
			}
			if (playerData2.Name != string.Empty)
			{
				victim2 = playerData2.Name;
			}
			if ((bool)guiBadgeManager)
			{
				guiBadgeManager.PushKillText(text + "(" + killerscore + ")", victim2, killtype);
			}
			if (killer == UnitZ.gameManager.PlayerNetID && (bool)UnitZ.Hud.KillFeed)
			{
				UnitZ.Hud.KillFeed.Kill(victim2, text, killerscore, killtype);
			}
		}
	}

	private void UNetVersion()
	{
	}

	protected static void InvokeCmdCmdScoreUpdate(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdScoreUpdate called on client.");
		}
		else
		{
			((ScoreManager)obj).CmdScoreUpdate((int)reader.ReadPackedUInt32(), (int)reader.ReadPackedUInt32(), (int)reader.ReadPackedUInt32());
		}
	}

	public void CallCmdScoreUpdate(int killer, int victim, int score)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdScoreUpdate called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdScoreUpdate(killer, victim, score);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdScoreUpdate);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.WritePackedUInt32((uint)killer);
		networkWriter.WritePackedUInt32((uint)victim);
		networkWriter.WritePackedUInt32((uint)score);
		SendCommandInternal(networkWriter, 0, "CmdScoreUpdate");
	}

	protected static void InvokeRpcRpcScoreReceived(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("RPC RpcScoreReceived called on server.");
		}
		else
		{
			((ScoreManager)obj).RpcScoreReceived((int)reader.ReadPackedUInt32(), (int)reader.ReadPackedUInt32(), (int)reader.ReadPackedUInt32());
		}
	}

	public void CallRpcScoreReceived(int killer, int victim, int score)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("RPC Function RpcScoreReceived called on client.");
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)2);
		networkWriter.WritePackedUInt32((uint)kRpcRpcScoreReceived);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.WritePackedUInt32((uint)killer);
		networkWriter.WritePackedUInt32((uint)victim);
		networkWriter.WritePackedUInt32((uint)score);
		SendRPCInternal(networkWriter, 0, "RpcScoreReceived");
	}

	public override float GetNetworkSendInterval()
	{
		return 1f;
	}

	static ScoreManager()
	{
		kCmdCmdScoreUpdate = 1594777568;
		NetworkBehaviour.RegisterCommandDelegate(typeof(ScoreManager), kCmdCmdScoreUpdate, InvokeCmdCmdScoreUpdate);
		kRpcRpcScoreReceived = -266997982;
		NetworkBehaviour.RegisterRpcDelegate(typeof(ScoreManager), kRpcRpcScoreReceived, InvokeRpcRpcScoreReceived);
		NetworkCRC.RegisterBehaviour("ScoreManager", 0);
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool result = default(bool);
		return result;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
	}
}
